<?php
namespace backend\modules\spread\models;

use common\libs\Constants;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\activeRecords\SpreadInfo;

/**
 * SpreadInfoSearch represents the model behind the search form of `backend\models\activeRecords\SpreadInfo`.
 */
class SpreadInfoSearch extends SpreadInfo
{
    public $server_name;
    public $game_name;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'game_id', 'server_id'], 'integer'],
            [['server_name','game_name','guild_sign', 'spread_key_1', 'spread_key_2', 'spread_url', 'spread_surl'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SpreadInfo::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'game_id' => $this->game_id,
            'server_id' => $this->server_id,
        ]);

        $query->andFilterWhere(['like', 'guild_sign', $this->guild_sign])
            ->andFilterWhere(['like', 'spread_key_1', $this->spread_key_1])
            ->andFilterWhere(['like', 'spread_key_2', $this->spread_key_2])
            ->andFilterWhere(['like', 'spread_url', $this->spread_url])
            ->andFilterWhere(['like', 'spread_surl', $this->spread_surl])
            ->joinWith('game')
            ->andFilterWhere(['like', 't_game.game_name', $this->game_name])
            ->joinWith('server')
            ->andFilterWhere(['like', 't_server.server_name', $this->server_name]);
        $auth = Yii::$app->authManager;
        $roleGuildManager = Constants::ROLE_GUILD_MANAGER;
        $roleGuildChannel = Constants::ROLE_GUILD_CHANNEL;
        if(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildManager])){
            $query->andFilterWhere(['like', 'spread_key_2', Yii::$app->user->getIdentity()->position]);
        }elseif(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildChannel])){
            $query->andFilterWhere(['like', 'spread_key_2', Yii::$app->user->getIdentity()->position]);
        }else{
            $query->andFilterWhere(['like', 'guild_sign', $this->guild_sign]);
            $query->andFilterWhere(['like', 'spread_key_2', $this->spread_key_2]);
        }
        return $dataProvider;
    }
}
